package org.glickr

import org.glickr.enums.FlickrSizeSuffix

class flickrTagLib {

    def photoImage = {attrs, body ->
        FlickrSizeSuffix suffix = (attrs.sizeSuffix?:null)

        if (attrs.photo && attrs.photo.getSourceUrl(suffix)) {
            def img = [
                href:   (attrs.href != null ? (attrs.href?.size()>0 ? attrs.href : null) : attrs.photo.shortUrl),
                target: (attrs.target   ?: "_FLICKR"),
                title:  (attrs.title    ?: attrs.photo.title),
                alt:    (attrs.alt      ?: attrs.photo.title),
                width:  (attrs.width    ?: attrs.photo.getSourceWidth(suffix)),
                height: (attrs.height   ?: attrs.photo.getSourceHeight(suffix)),
                css:    (attrs.css      ?: null),
                src:    attrs.photo.getSourceUrl(suffix)
            ]
            out << render(plugin:"glickr", template:"/flickrTagLib/image", model:[img:img])
        }
    }

    def peopleImage = {attrs, body ->
        if (attrs.people && attrs.people.iconUrl) {
            def img = [
                href:   (attrs.href     ?: attrs.people.photosUrl),
                target: (attrs.target   ?: "_FLICKR"),
                title:  (attrs.title    ?: attrs.people.username),
                alt:    (attrs.alt      ?: attrs.people.username),
                width:  (attrs.width    ?: 48),
                height: (attrs.height   ?: 48),
                css:    (attrs.css      ?: null),
                src:    attrs.people.iconUrl
            ]
            out << render(plugin:"glickr", template:"/flickrTagLib/image", model:[img:img])
        }
    }

    def photoSetBrowser = {attrs, body ->
        if (attrs.photoSet) {

            out << '<br />'
            out << attrs.photoSet

            def set = [
                    pageFirst: 1,
                    pageCurrent: attrs.photoSet.page,
                    pageLast: Math.ceil(attrs.photoSet.total / attrs.photoSet.perPage),
                    perPage: attrs.photoSet.perPage,

                    params: attrs.photoSet.params
            ]

            out << render(plugin:"glickr", template:"/flickrTagLib/browser", model:[set:set])
        }
    }
}
